package com.cakeclient.Model;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.cakeclient.database.DataSource;
import com.cakeclient.database.IDatabasable;
import com.cakeclient.database.PaymentTypeTable;

import java.util.ArrayList;
import java.util.List;

public class PaymentType implements IDatabasable {
    private Long idDB;
    private Long remoteID;
    private String name;
    private float bookingFee;

    private PaymentType(DataSource dataSource, Cursor cursor) {
        idDB = cursor.getLong(cursor.getColumnIndexOrThrow(PaymentTypeTable.ID));
        setRemoteID(cursor.getLong(cursor.getColumnIndexOrThrow(PaymentTypeTable.REMOTE_ID)));
        name = cursor.getString(cursor.getColumnIndexOrThrow(PaymentTypeTable.NAME));
        bookingFee = cursor.getFloat(cursor.getColumnIndexOrThrow(PaymentTypeTable.BOOKINGFEE));
    }

    public PaymentType(com.cakeclient.clientsoap.contract.PaymentType paymentType) {
        this.remoteID = Long.valueOf(paymentType.id);
        this.name = paymentType.name;
        this.bookingFee = paymentType.bookingFee;
    }

    public Long getRemoteID() {
        return remoteID;
    }

    private void setRemoteID(Long remoteID) {
        this.remoteID = remoteID;
    }

    public Long getIdDB() {
        return idDB;
    }

    public void setIdDB(Long idDB) {
        this.idDB = idDB;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getBookingFee() {
        return bookingFee;
    }

    public void setBookingFee(float bookingFee) {
        this.bookingFee = bookingFee;
    }

    @Override
    public ContentValues getValues() {
        ContentValues values = new ContentValues();
        values.put(PaymentTypeTable.REMOTE_ID, this.getRemoteID());
        values.put(PaymentTypeTable.NAME, this.name);
        values.put(PaymentTypeTable.BOOKINGFEE, this.bookingFee);
        return values;
    }


    @Override
    public void saveToDatabase(Context context) {
        //Protection anti-doublons
        PaymentType bis = null;
        if (this.getIdDB() == null && this.getRemoteID() != null && (bis = PaymentType.getOneFromRemoteID(context, this.getRemoteID())) != null) {
            this.setIdDB(bis.getIdDB());
        }

        DataSource dataSource = new DataSource(context);
        dataSource.open();
        if (this.getIdDB() == null) {
            dataSource.insert(this, PaymentTypeTable.getInstance());
        } else {
            dataSource.update(this, PaymentTypeTable.getInstance());
        }
        dataSource.close();
    }

    @Override
    public void removeFromDatabase(Context context) {
        if (this.getIdDB() != null) {
            DataSource dataSource = new DataSource(context);
            dataSource.open();
            dataSource.delete(this, PaymentTypeTable.getInstance());
            dataSource.close();
        }
    }


    public static PaymentType getOne(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOne(idDB, PaymentTypeTable.getInstance());
        cursor.moveToFirst();
        PaymentType item = null;
        while (!cursor.isAfterLast()) {
            item = new PaymentType(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static PaymentType getOneFromRemoteID(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOneFromRemoteID(idDB, PaymentTypeTable.getInstance());
        cursor.moveToFirst();
        PaymentType item = null;
        while (!cursor.isAfterLast()) {
            item = new PaymentType(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static List<PaymentType> getAll(Context context) {
        List<PaymentType> result = new ArrayList<PaymentType>();
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchAll(PaymentTypeTable.getInstance());
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            result.add(new PaymentType(dataSource, cursor));
            cursor.moveToNext();
        }
        dataSource.close();
        return result;
    }

    public static PaymentType getOne(DataSource dataSource, long idDB) {
        Cursor cursor = dataSource.fetchOne(idDB, PaymentTypeTable.getInstance());
        cursor.moveToFirst();
        PaymentType item = null;
        while (!cursor.isAfterLast()) {
            item = new PaymentType(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        return item;
    }
}
